package com.example.teadoctor;

import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

//我的收藏
public class Collections extends AppCompatActivity {

    private ArrayAdapter<String> adapter;
    private SharedPreferences sp;
    private List<String> favoriteList;
    ListView listView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        sp = getSharedPreferences("favorite", MODE_PRIVATE);
        // 获取已收藏的name数组，若无则创建新数组
        Set<String> favoriteNames = sp.getStringSet("favoriteNames", new HashSet<String>());
        assert favoriteNames != null;
        favoriteList = new ArrayList<String>(favoriteNames);

        setContentView(R.layout.page_collections);


        //设置数据源
        adapter = new ArrayAdapter<String>(Collections.this, android.R.layout.simple_expandable_list_item_1, favoriteList);
        listView = findViewById(R.id.Pst_my_collections);
        try {    listView.setAdapter(adapter);
        } catch (Exception e) {
            e.printStackTrace();
        }

        //点击记录跳转，要传递数据过去，具体展示
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String selectedRecord = (String) parent.getItemAtPosition(position); // 获取所选中的记录
                // 解析记录中的数据
                String name = favoriteList.get(position);
                if (name.length() <= 5) {
                    // 将茶名通过intent传递给要跳转的页面
                    Intent intent = new Intent(Collections.this, Information.class);
                    intent.putExtra("name", name);
                    startActivity(intent);
                } else if(name.startsWith("如何")){
                    // 将经营标题通过intent传递给要跳转的页面
                    Intent intent = new Intent(Collections.this, BusinessInformation.class);
                    intent.putExtra("title", name);
                    startActivity(intent);
                }
            }
        });

        // 长按某一行显示删除选项
        listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
                AlertDialog.Builder builder = new AlertDialog.Builder(Collections.this);
                builder.setTitle("删除收藏")
                        .setMessage("确定要删除该收藏吗？")
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                // 删除该收藏
                                favoriteList.remove(position);
                                // 更新shared preferences
                                SharedPreferences.Editor editor = sp.edit();
                                editor.putStringSet("favoriteNames", new HashSet<String>(favoriteList));
                                editor.apply();
                                // 更新listview
                                adapter.notifyDataSetChanged();
                            }
                        })
                        .setNegativeButton("取消", null)
                        .show();
                return true;
            }
        });

        Button deleteBtn = findViewById(R.id.btn_delete);
        deleteBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(Collections.this);
                builder.setTitle("确认删除所有收藏吗？");
                builder.setPositiveButton("继续删除", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        favoriteList.clear(); // 清空favoriteList
                        adapter.notifyDataSetChanged(); // 刷新ListView
                        SharedPreferences.Editor editor = sp.edit();
                        editor.clear(); // 清空SharedPreferences
                        editor.apply(); // 提交修改
                    }
                });
                builder.setNegativeButton("取消", null);
                builder.show();
            }
        });
    }
    @Override
    protected void onResume() {
        super.onResume();
        //页面发生改变时，重新读取SharedPreferences中的数据，更新我的收藏
        /*和删除，跳转功能有所冲突
        SharedPreferences sp = getSharedPreferences("favorite", MODE_PRIVATE);
        Set<String> favoriteNames = sp.getStringSet("favoriteNames", new HashSet<String>());
        assert favoriteNames != null;
        List<String> favoriteList = new ArrayList<String>(favoriteNames);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(Collections.this, android.R.layout.simple_list_item_1, favoriteList);
        ListView listView = findViewById(R.id.Pst_my_collections);
        try {    listView.setAdapter(adapter);
        } catch (Exception e) {
            e.printStackTrace();
        }
        */
    }
}
